<template>
  <div class="setting-body">
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <div class="custom-tips">
        基础设置
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="12">
          <el-form-item
            label="是否定时刷新"
            prop="isRefresh"
            :rules="[{ required: true, message: '请选择是否刷新', trigger: 'change' }]"
          >
            <el-radio-group v-model="form.isRefresh">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="form.isRefresh === true" :span="12">
          <el-form-item
            label="间隔时间(秒)"
            prop="refreshTime"
            :rules="{ required: true, message: '请输入间隔时间(秒)', trigger: 'blur' }"
            style="width: 100%"
          >
            <el-input-number v-model="form.refreshTime" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="默认显示条数"
            prop="size"
            :rules="[{ required: true, message: '默认显示条数', trigger: 'blur' }]"
          >
            <el-input-number v-model="form.size" :min="1" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="是否自定义排序"
            prop="isCustomShort"
            :rules="[{ required: true, message: '是否自定义排序', trigger: 'change' }]"
          >
            <el-radio-group v-model="form.isCustomShort">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="custom-tips">
        列表项设置(注意列宽之和必须等于24)
        <div class="button-add">
          <el-button type="text" icon="el-icon-plus" @click="doAdd">新增</el-button>
        </div>
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col v-for="(item,index) in form.data" :key="'data-'+index" :span="24">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item
                label="显示名称"
                :prop="'data.'+index+'.label'"
                :rules="[{ required: true, message: '请输入显示名称', trigger: 'blur' }]"
              >
                <el-input v-model="item.label" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="关联字段"
                :prop="'data.'+index+'.column'"
                :rules="[{ required: true, message: '请选择关联字段', trigger: 'change' }]"
              >
                <el-select v-model="item.column" clearable placeholder="请选择关联字段">
                  <el-option
                    v-for="obj in jxx"
                    :key="obj"
                    :label="obj"
                    :value="obj"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item
                label="显示宽度"
                :prop="'data.'+index+'.width'"
                :rules="[{ required: true, message: '请选择关联字段', trigger: 'blur' }]"
              >
                <el-input-number v-model="item.width" :max="24" :min="1" />
              </el-form-item>
            </el-col>
            <el-col :span="4" style="text-align: center">
              <el-button type="text" icon="el-icon-delete" size="mini" @click="toDelete(item)">删除</el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <div class="custom-tips">
        查询项设置
        <div class="button-add">
          <el-button type="text" icon="el-icon-plus" @click="doAddQuery">新增</el-button>
        </div>
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col v-for="(item,index) in form.query" :key="'query-'+index" :span="24">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item
                label="显示名称"
                :prop="'query.'+index+'.label'"
                :rules="[{ required: true, message: '请输入显示名称', trigger: 'blur' }]"
              >
                <el-input v-model="item.label" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="关联字段"
                :prop="'query.'+index+'.column'"
                :rules="[{ required: true, message: '请选择关联字段', trigger: 'change' }]"
              >
                <el-select v-model="item.column" clearable placeholder="请选择关联字段">
                  <el-option
                    v-for="obj in form.data"
                    :key="obj.column"
                    :label="obj.label"
                    :value="obj.column"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="字段类型"
                :prop="'query.'+index+'.type'"
                :rules="[{ required: true, message: '请选择字段类型', trigger: 'change' }]"
              >
                <el-select v-model="item.type" clearable placeholder="请选择字段类型">
                  <el-option
                    v-for="obj in controlDataList"
                    :key="obj.id"
                    :label="obj.name"
                    :value="obj.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="运算符"
                :prop="'query.'+index+'.ysf'"
                :rules="[{ required: true, message: '请选择运算符', trigger: 'change' }]"
              >
                <el-select v-model="item.ysf" clearable placeholder="请选择运算符">
                  <el-option
                    v-for="obj in gxfList"
                    :key="obj.id"
                    :label="obj.name"
                    :value="obj.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item
                label="显示宽度"
                :prop="'query.'+index+'.width'"
                :rules="[{ required: true, message: '请选择关联字段', trigger: 'blur' }]"
              >
                <el-input-number v-model="item.width" :max="24" :min="1" />
              </el-form-item>
            </el-col>
            <el-col :span="4" style="text-align: center">
              <el-button type="text" icon="el-icon-delete" size="mini" @click="doDeleteQuery(item)">删除</el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <div class="custom-tips">
        排序设置
        <div class="button-add">
          <el-button type="text" icon="el-icon-plus" @click="doAddOrder">新增</el-button>
        </div>
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col v-for="(item,index) in form.shortColumn" :key="'shortColumn-'+index" :span="24">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item
                label="排序字段"
                :prop="'shortColumn.'+index+'.pxx'"
                :rules="[{ required: true, message: '请选择排序字段', trigger: 'change' }]"
              >
                <el-select v-model="item.pxx" clearable placeholder="请选择排序字段">
                  <el-option
                    v-for="obj in form.data"
                    :key="obj.column"
                    :label="obj.label"
                    :value="obj.column"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="排序方式"
                :prop="'shortColumn.'+index+'.order'"
                :rules="[{ required: true, message: '请选择排序方式', trigger: 'change' }]"
                style="width: 100%"
              >
                <el-select v-model="item.order" filterable clearable>
                  <el-option
                    v-for="(data,index) in orders"
                    :key="index"
                    :label="data.label"
                    :value="data.value"
                  />
                </el-select>
                <el-button type="primary" size="mini" @click="removeOrder(item)">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <div class="custom-tips">
        详情设置
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="12">
          <el-form-item label="是否打开详情页" prop="isOpenEntity" :rules="[{ required: true, message: '请选择是否打开详情页', trigger: 'change' }]">
            <el-radio-group v-model="form.isOpenEntity">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="form.isOpenEntity" :gutter="20" style="margin-top: 10px">
        <el-col :span="12">
          <el-form-item label="详情页打开方式" prop="entityOpenType" :rules="[{ required: true, message: '请选择详情页打开方式', trigger: 'change' }]">
            <el-select v-model="form.entityOpenType" clearable placeholder="请选择详情页打开方式">
              <el-option
                v-for="obj in openType"
                :key="'entity-'+obj.id"
                :label="obj.label"
                :value="obj.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="form.entityOpenType === 1" :span="24">
          <el-form-item
            label="地址参数"
            prop="entityUrl"
            :rules="[{ required: true, message: '请选择地址参数', trigger: 'change' }]"
          >
            <el-select v-model="form.entityUrl" clearable placeholder="请选择地址参数">
              <el-option
                v-for="obj in jxx"
                :key="obj"
                :label="obj"
                :value="obj"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="form.entityOpenType === 3" :span="12">
          <el-form-item label="详情模版" prop="entityTemplateId" :rules="[{ required: true, message: '请选择地址参数', trigger: 'change' }]">
            <el-select v-model="form.entityTemplateId" clearable placeholder="请选择地址参数" @change="entityChange">
              <el-option
                v-for="obj in entityTemp"
                :key="obj.id"
                :label="obj.name"
                :value="obj.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <div v-if="form.isOpenEntity === true && form.entityOpenType === 2" class="custom-tips">
        参数设置
        <div class="button-add">
          <el-button type="text" icon="el-icon-plus" @click="doAddParams">新增</el-button>
        </div>
      </div>
      <el-row v-if="form.isOpenEntity === true && form.entityOpenType === 2" :gutter="20" style="margin-top: 10px">
        <el-col :span="24">
          <el-row v-for="(item,index) in form.params" :key="'pp-'+index" :gutter="20">
            <el-col :span="10">
              <el-form-item
                label="参数"
                :prop="'params.'+ index+'.code'"
                :rules="[{ required: true, message: '请输入参数名称', trigger: 'blur' }]"
              >
                <el-input v-model="item.code" clearable placeholder="请输入参数名称" />
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item
                label="请选择关联字段"
                :prop="'params.'+index+'.column'"
                :rules="[{ required: true, message: '请选择关联字段', trigger: 'change' }]"
              >
                <el-select v-model="item.column" clearable placeholder="请选择关联字段">
                  <el-option
                    v-for="obj in jxx"
                    :key="obj"
                    :label="obj"
                    :value="obj"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4" style="text-align: center">
              <el-button type="text" icon="el-icon-delete" size="mini" @click="removeParams(item)">删除</el-button>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="地址参数"
            prop="entityUrl"
            :rules="[{ required: true, message: '请输入地址参数', trigger: 'blur' }]"
          >
            <el-input v-model="form.entityUrl" clearable placeholder="请输入打开地址，地址中参数请用${}包裹" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="form.entityOpenType === 3 && form.isOpenEntity === true" :gutter="20" :span="12" style="margin-top: 10px">
        <el-col v-for="(item,index) in form.entityParams" :key="'ee-'+index" :span="12">
          <el-form-item
            :label="item.name"
            :prop="'entityParams.'+index+'.value'"
            :rules="[{ required: true, message: '请选择关联字段', trigger: 'change' }]"
          >
            <el-select v-model="item.value" clearable placeholder="请选择关联字段">
              <el-option
                v-for="obj in jxx"
                :key="obj"
                :label="obj"
                :value="obj"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="custom-button">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </div>
</template>

<script>
import { queryAllControl, queryAllGxf } from '@/api/portal/manager/setting';
import { queryAllDataSource, queryAllDataBySystem } from '@/api/portal/manager/analysis';
import { updateObject } from '@/api/portal/manager/columnTemp';
import { queryAll } from '@/api/portal/manager/entityTemp/index';
export default {
  name: 'PcNewListTempV1',
  props: {
    data: {
      type: Object,
      default: function() {
        return {};
      }
    }
  },
  data() {
    return {
      loading: false,
      controlDataList: [],
      gxfList: [],
      jxx: [],
      entityTemp: [],
      openType: [
        { id: 1, value: 1, label: '地址参数' },
        { id: 2, value: 2, label: '地址传参' },
        { id: 3, value: 3, label: '详情模版' }
      ],
      status: [
        { id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }
      ],
      types: [
        { id: 0, value: 'custom', label: '自定义地址' },
        { id: 1, value: 'system', label: '默认地址' }
      ],
      orders: [
        { id: 0, value: 'DESC', label: '降序' },
        { id: 1, value: 'ASC', label: '升序' }
      ],
      form: {
        isOpenEntity: true, // 是否打开详情页
        isOpenMore: true, // 是否显示更多
        isRefresh: false, // 是否定时刷新
        isCustomShort: false, // 是否自定义排序
        refreshTime: 20, // 定时刷新时间单位秒
        openMoreType: '', // 更多打开方式，外部链接，还是更多页面
        openMoreUrl: '', // 更多显示地址
        size: 5, // 每页显示几条
        entityTemplateId: null, // 详情页模版编号
        data: [], // 数据显示项配置
        shortColumn: [], // 排序字段及排序方式
        query: [], // 查询条件设置
        entityParams: [], // 详情参数设置
        params: [],
        entityOpenType: null, // 详情页打开方式
        entityUrl: '', // 详情地址
        entityTemp: null
      }
    };
  },
  watch: {
    data: {
      handler(newName, oldName) {
        if (undefined !== newName && newName.sjlx !== null) {
          if (newName.sjlx === 1) {
            this.initDataJxx();
          } else if (newName.sjlx === 2) {
            this.initSystemJxx();
          }
          if (undefined !== newName.pzx && newName.pzx !== '' && newName.pzx !== null) {
            this.form = JSON.parse(newName.pzx);
            if (this.form.data === undefined) {
              this.form.data = [];
            }
            if (this.form.shortColumn === undefined) {
              this.form.shortColumn = [];
            }
            if (this.form.query === undefined) {
              this.form.query = [];
            }
            if (this.form.entityParams === undefined) {
              this.form.entityParams = [];
            }
            if (this.form.params === undefined || this.form.params === null) {
              this.form.params = [];
            }
          }
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  created() {
    this.initControl();
    this.initGxf();
    this.initAllTemp();
  },
  methods: {
    doAdd() {
      const item = { label: '', column: '', width: null };
      this.form.data.push(item);
    },
    doAddQuery() {
      const item = { label: '', column: '', type: null, ysf: null };
      this.form.query.push(item);
    },
    doAddOrder() {
      const item = { pxx: '', order: '', id: null };
      this.form.shortColumn.push(item);
    },
    doAddParams() {
      const item = { code: '', column: '' };
      this.form.params.push(item);
    },
    doDeleteQuery(item) {
      const index = this.form.query.indexOf(item);
      if (index !== -1) {
        this.form.query.splice(index, 1);
      }
    },
    toDelete(item) {
      const index = this.form.data.indexOf(item);
      if (index !== -1) {
        this.form.data.splice(index, 1);
      }
    },
    removeOrder(item) {
      const index = this.form.shortColumn.indexOf(item);
      if (index !== -1) {
        this.form.shortColumn.splice(index, 1);
      }
    },
    removeParams(item) {
      const index = this.form.params.indexOf(item);
      if (index !== -1) {
        this.form.params.splice(index, 1);
      }
    },
    save() {
      this.loading = true;
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.data.pzx = JSON.stringify(this.form);
          updateObject(this.data).then(res => {
            this.close();
          }).catch(() => {
            this.loading = false;
          });
        } else {
          this.loading = false;
        }
      });
    },
    close() {
      this.loading = false;
      Object.assign(this.$data.form, this.$options.data.call(this).form);
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    },
    initControl() {
      queryAllControl().then(res => {
        const { data } = res;
        this.controlDataList = data;
      });
    },
    initGxf() {
      queryAllGxf().then(res => {
        const { data } = res;
        this.gxfList = data;
      });
    },
    initDataJxx() {
      queryAllDataSource({ id: this.data.sjx }).then(res => {
        const { data } = res;
        this.jxx = data;
      });
    },
    initSystemJxx() {
      queryAllDataBySystem({ id: this.data.sjx }).then(res => {
        const { data } = res;
        this.jxx = data;
      });
    },
    initAllTemp() {
      queryAll().then(res => {
        const { data } = res;
        this.entityTemp = data.data;
      }).catch(() => {
        this.entityTemp = [];
      });
    },
    entityChange(data) {
      const entity = this.entityTemp.filter(item => {
        if (item.id === data) {
          return item;
        }
      });
      if (entity !== undefined && entity !== null && entity.length > 0) {
        const param = entity[0].params;
        this.form.entityTemp = entity[0];
        this.form.entityParams = [];
        if (undefined !== param && param !== null) {
          const newParams = JSON.parse(param).map(item => {
            item.test = false;
            item.value = '';
            return item;
          });
          newParams.forEach(item => {
            this.form.entityParams.push(item);
          });
        } else {
          this.form.entityParams = [];
        }
      }
    }
  }
};
</script>

<style scoped>
.setting-body {
  width: 100%;
}

.custom-tips {
  width: 100%;
  min-height: 40px;
  line-height: 40px;
  text-align: left;
  padding: 8px 16px;
  background-color: #ecf8ff;
  border-radius: 4px;
  border-left: 5px solid #50bfff;
}

.button-add {
  width: 50px;
  height: 40px;
  float: right;
  text-align: center;
  line-height: 40px;
  margin-right: 20px;
}
.custom-button {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: right;
}
</style>
